using System;
using System.Collections.Generic;
using UnityEngine;

namespace TC
{
    public class EventCenter
    {
        public delegate void EventHandler();
        public delegate void EventHandler<T0>(T0 t0);
        public delegate void EventHandler<T0, T1>(T0 t0, T1 t1);
        public delegate void EventHandler<T0, T1, T2>(T0 t0, T1 t1, T2 t2);
        public delegate void EventLuaHandler(object param);

        private static EventCenter _instance;
        public static EventCenter Instance
        {
            get { return _instance ?? (_instance = new EventCenter()); }
        }

        private Dictionary<int, Delegate> eventList = new Dictionary<int, Delegate>();

        public static void Destroy()
        {
            if (_instance != null)
            {
                _instance.eventList.Clear();
            }
        }

        private void _register(int type, Delegate handler)
        {
            Delegate callbacks;
            if (eventList.TryGetValue(type, out callbacks))
            {
                Delegate[] arr = callbacks.GetInvocationList();
                foreach (var e in arr)
                {
                    if (e == handler)
                    {
#if UNITY_EDITOR
                        Debug.Log(string.Format("EventCenter Register duplicate {0}", type.ToString()));
#endif
                        return;
                    }
                }

                eventList[type] = Delegate.Combine(callbacks, handler);
            }
            else
            {
                eventList.Add(type, handler);
            }
        }

        private void _unregister(int type, Delegate handler)
        {
            if (eventList.ContainsKey(type))
            {
                eventList[type] = Delegate.Remove(eventList[type], handler);

                if (eventList[type] == null)
                {
                    eventList.Remove(type);
                }
            }
        }

        #region register and unregister
        public void Register(int type, Delegate handler)
        {
            _register(type, handler);
        }

        public void Register(int type, EventHandler handler)
        {
            _register(type, handler);
        }

        public void Register<T0>(int type, EventHandler<T0> handler)
        {
            _register(type, handler);
        }

        public void Register<T0, T1>(int type, EventHandler<T0, T1> handler)
        {
            _register(type, handler);
        }


        public void Register<T0>(int type, EventHandler<long, T0> handler)
        {
            _register(type, handler);
        }

        public void Register<T0, T1, T2>(int type, EventHandler<T0, T1, T2> handler)
        {
            _register(type, handler);
        }

        public void Unregister(int type, Delegate handler)
        {
            _unregister(type, handler);
        }

        public void Unregister(int type, EventHandler handler)
        {
            _unregister(type, handler);
        }

        public void Unregister<T0>(int type, EventHandler<T0> handler)
        {
            _unregister(type, handler);
        }

        public void Unregister<T0>(int type, EventHandler<long, T0> handler)
        {
            _unregister(type, handler);
        }

        public void Unregister<T0, T1>(int type, EventHandler<T0, T1> handler)
        {
            _unregister(type, handler);
        }

        public void Unregister<T0, T1, T2>(int type, EventHandler<T0, T1, T2> handler)
        {
            _unregister(type, handler);
        }

        public void SendEvent(int type)
        {
            Delegate callbacks;
            if (eventList.TryGetValue(type, out callbacks))
            {
#if UNITY_EDITOR
                if (callbacks.GetType() != typeof(EventHandler))
                {
                    Debug.Log(string.Format("EventCenter SendEvent mismatch {0}", type.ToString()));
                }
#endif
                (callbacks as EventHandler)?.Invoke();
            }
        }

        public void SendEvent<T0>(int type, T0 arg0)
        {
            Delegate callbacks;
            if (eventList.TryGetValue(type, out callbacks))
            {
#if UNITY_EDITOR
                if (callbacks.GetType() != typeof(EventHandler<T0>))
                {
                    Debug.Log(string.Format("EventCenter SendEvent mismatch {0}", type.ToString()));
                }
#endif
                (callbacks as EventHandler<T0>)?.Invoke(arg0);
            }
        }

        public void SendEvent<T0, T1>(int type, T0 arg0, T1 arg1)
        {
            Delegate callbacks;
            if (eventList.TryGetValue(type, out callbacks))
            {
#if UNITY_EDITOR
                if (callbacks.GetType() != typeof(EventHandler<T0, T1>))
                {
                    Debug.Log(string.Format("EventCenter SendEvent mismatch {0}", type.ToString()));
                }
#endif
                (callbacks as EventHandler<T0, T1>)?.Invoke(arg0, arg1);
            }
        }

        public void SendEvent<T0, T1, T2>(int type, T0 arg0, T1 arg1, T2 arg2)
        {
            Delegate callbacks;
            if (eventList.TryGetValue(type, out callbacks))
            {
#if UNITY_EDITOR
                if (callbacks.GetType() != typeof(EventHandler<T0, T1, T2>))
                {
                    Debug.Log(string.Format("EventCenter SendEvent mismatch {0}", type.ToString()));
                }
#endif
                (callbacks as EventHandler<T0, T1, T2>)?.Invoke(arg0, arg1, arg2);
            }
        }


        #endregion
    }
}
